package ua.mykola.graph;

public class Edge {
	private final Vertex vA;
	private final Vertex vB;
	private final int weight;

	public Edge(Vertex vA, Vertex vB, int weight) {
		this.vA = vA;
		this.vB = vB;
		this.weight = weight;
	}

	public Edge(String vA, String vB, int weight) {
		this(new Vertex(vA), new Vertex(vB), weight);
	}

	@Override
	public String toString() {
		return "Edge [" + vA + " " + vB + " " + weight + "]";
	}

	public Edge(Vertex sideA, Vertex sideB) {
		this.vA = sideA;
		this.vB = sideB;
		this.weight = 0;
	}

	public Vertex getVertexA() {
		return vA;
	}

	public Vertex getVertexB() {
		return vB;
	}

	public int getWeight() {
		return weight;
	}

	public Vertex getOtherSide(Vertex vertex) {
		if (vertex.equals(vA)) {
			return vB;
		} else {
			return vA;
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((vA == null) ? 0 : vA.hashCode());
		result = prime * result + ((vB == null) ? 0 : vB.hashCode());
		result = prime * result + weight;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Edge other = (Edge) obj;
		if (vA == null) {
			if (other.vA != null)
				return false;
		} else if (!vA.equals(other.vA))
			return false;
		if (vB == null) {
			if (other.vB != null)
				return false;
		} else if (!vB.equals(other.vB))
			return false;
		if (weight != other.weight)
			return false;
		return true;
	}

}
